﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;

using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.IDAL;
using OgilvyOne.UKNow.DAL;
using OgilvyOne.UKNow.DALFactory;
using ECCIT;
using ECCIT.Data;
using System.Collections;

namespace OgilvyOne.UKNow.BLL
{
    
    public  class WarmupBLL
    {
        public int LanguageID { get; set; }
        public WarmupBLL(int langID)
        {
            this.LanguageID = langID;
        }

        //根据作品id获得该对象的实体
        public WarmupModel GetWarmupById(int wid)
        {
            IDALWarmup use = DALFactory.WarmupDALFactory.Create(LanguageID);
            return use.GetWarmupModelByID(wid);
        }

        public WarmupModel GetWarmupById(object p)
        {
            throw new NotImplementedException();
        }
        /*修改人：穆凤凤
      * 修改时间：2012-02-08
      * 修改内容：
      *      添加方法,获得所有作品的对象实体
      *      有mff注释的为本次修改的代码
      */
        //mff
        //获得所有作品的对象实体
        public IList<WarmupModel> selectwarmups()
        {
            IDALWarmup use = WarmupDALFactory.Create(LanguageID);
            return use.GetAllWarmupModels();
            
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="keyword">关键字</param>
        /// <param name="langid">语言ID</param>
        /// <param name="mediatype">资源类型,0活动图片1活动视频2作品图片</param>
        /// <param name="indexpage">当前页号从0开始</param>
        /// <param name="numinpage">每页个数</param>
        /// <returns></returns>
        /// 
        //public List<MediaFileModel> returnSource(string keyword, int langid, int mediatype, int indexpage, int numinpage)
        //{
        //    indexpage = indexpage + 1;
        //    List<MediaFileModel> lmm = returnSource(keyword,langid);

        //    if (lmm.Count == 0)
        //    {
        //        return null;
        //    }
        //    else
        //    {
        //        var mmlist = from lm in lmm
        //                     where lm.MediType == mediatype
        //                     select lm;
        //        if (mmlist.Count() == 0)
        //        {
        //            return null;
        //        }
                
        //        int b = (indexpage - 1) * numinpage;
        //        int a = b + numinpage - 1;
        //        List<MediaFileModel> lmfm = new List<MediaFileModel>();
        //        lmfm = mmlist.ToList<MediaFileModel>();


        //        MediaFileModel[] lmd = new MediaFileModel[numinpage];
        //        if (lmfm.Count == 0)
        //        {
        //            return null;
        //        }
        //        else
        //        {
        //            lmfm.CopyTo(b, lmd, 0, (lmfm.Count < numinpage) ? lmfm.Count : numinpage);
        //        }
        //        if (lmd.Length != 0)
        //        {

        //            List<MediaFileModel> lml = lmd.ToList<MediaFileModel>();
        //            lml = lmd.ToList<MediaFileModel>();
        //            List<MediaFileModel> last = new List<MediaFileModel>();
        //            foreach (MediaFileModel mfmd in lml)
        //            {


        //                if (mfmd != null)
        //                {
        //                    last.Add(mfmd);
        //                }
        //            }
        //            return last;

        //        }
        //        else
        //        {
        //            return null;

        //        }
        //    }
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="keyword">关键字</param>
        ///// <param name="langid"></param>
        ///// <param name="mediatype"></param>
        ///// <returns></returns>
        //public int returnNum(string keyword, int langid, int mediatype)
        //{ 
        //    List<MediaFileModel> lmm = returnSource(keyword,langid);

        //    if (lmm.Count == 0)
        //    {
        //        return 0;
        //    }
        //    else
        //    {
        //        var mmlist = from lm in lmm
        //                     where lm.MediType == mediatype
        //                     select lm;
        //        if (mmlist.Count() == 0)
        //        {
        //            return 0;
        //        }
        //        else
        //        {
        //            return mmlist.Count();
        //        }
        //    }

        //}
        //public List<MediaFileModel> returnSource(string keyword, int langId)
        //{
        //    EventsImageBLL eib = new EventsImageBLL(langId);
        //    EventsVideoBLL evb = new EventsVideoBLL(langId);

        //    IList<EventsImageModel> iem = eib.GetKeyWordEventsImageModels(keyword);
        //    ArrayList al = new ArrayList();
        //    List<MediaFileModel> lmm = new List<MediaFileModel>();
        //    MediaFileModel mfm = null;
        //    List<string> imageNames = new List<string>();
        //    List<string> imageNames1 = new List<string>();
        //    List<string> videoNames = new List<string>();
        //    foreach (EventsImageModel eim in iem)
        //    {
        //        mfm = new MediaFileModel();
        //        mfm.ID = eim.EventsID;
        //        mfm.LanguageID = eim.LanguageID;
        //        mfm.Name = eim.Name;
        //        mfm.Path = eim.Path1;
        //        mfm.MediType = 0;//活动图片

        //        if (!imageNames.Contains(eim.Path1))
        //        {
        //            lmm.Add(mfm);
        //            imageNames.Add(eim.Path1);
        //            mfm = null;
        //        }
        //    }

        //    IList<EventsVideoModel> iev = evb(keyword);
        //    foreach (EventsVideoModel evm in iev)
        //    {
        //        mfm = new MediaFileModel();
        //        mfm.ID = evm.EventsID;
        //        mfm.LanguageID = evm.LanguageID;
        //        mfm.Name = evm.Name;
        //        mfm.Path = evm.ThumbnailImagePath;
        //        mfm.MediType = 1;//活动视频

        //        if (!imageNames1.Contains(evm.ThumbnailImagePath))
        //        {
        //            lmm.Add(mfm);
        //            imageNames1.Add(evm.ThumbnailImagePath);
        //            mfm = null;
        //        }
        //    }

        //    List<WarmupModel> lwm = selectwarmupspushNkeyword(keyword);
        //    if (lwm != null)
        //    {
        //        foreach (WarmupModel wm in lwm)
        //        {
        //            mfm = new MediaFileModel();
        //            mfm.ID = wm.ID;
        //            mfm.LanguageID = wm.LanguageID;

        //            mfm.Path = wm.ImagePath;
        //            mfm.MediType = 2;//作品

        //            if (!videoNames.Contains(wm.ImagePath))
        //            {
        //                lmm.Add(mfm);
        //                videoNames.Add(wm.ImagePath);
        //                mfm = null;
        //            }

        //        }
        //    }
        //    return lmm;
        //}

        public List<WarmupModel> selectwarmupspushNkeyword( string keyword)
        {
            IList<WarmupModel> selmups=selectwarmups();
            List<WarmupModel> lwm = new List<WarmupModel>();
            foreach (WarmupModel wm in selmups)
            {
                if (wm.State==1&&wm.Description.Replace(keyword,"a")!=wm.Description)
                {
                    lwm.Add(wm);
                }
            }
            if (lwm.Count != 0)
            {
                return lwm;
            }
            else
            {
                return null;
            }
            
        }
        //mff
        //修个单个作品的发布状态
        public void modPubOne(int wid)
        {
            List<int?> list = new List<int?>();
            list.Add(wid);

            IDALWarmup use = WarmupDALFactory.Create(LanguageID);

            WarmupModel model = use.GetWarmupModelByID(wid); //查看该作品的当前发布状态
            if (model.State > 0) //该作品的当前为发布状态
            {
                use.UnPublish(list);
                //设置为该作品的state为0 不发布 (传递参数 eid 和 int=0)
            }
            else
            {
                use.Publish(list);
                //设置该作品的state为1 发布     (传递参数 eid 和 int=1)
            }
        }
        //删除单个作品
        public ResultInfo delWarmup(int wid)
        {
            IList<int?> list = new List<int?>();
            list.Add(wid);
            IDALWarmup use = WarmupDALFactory.Create(LanguageID);
            return use.Delete(list);
        }

        public ResultInfo DeleteUploadedImage(string fileName)
        {
            ResultInfo result = new ResultInfo(false);
            string dir = HttpContext.Current.Server.MapPath("/uploadfiles/image");
            string filePath = Path.Combine(dir, fileName);
            try
            {
                if (File.Exists(filePath))
                    File.Delete(filePath);

                result.Result = true;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 删除评论上传的图片
        /// </summary>
        /// <param name="fileName">文件的名字</param>
        /// <returns></returns>
        public ResultInfo DeleteCommentUploadedImage(string fileName)
        {
            ResultInfo result = new ResultInfo(false);
            string dir = HttpContext.Current.Server.MapPath(" /uploadfiles/front_image");
            string filePath = Path.Combine(dir, fileName);
            try
            {
                if (File.Exists(filePath))
                    File.Delete(filePath);

                result.Result = true;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return result;
        }
       

        /// <summary>
        ///     保存作品
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ResultInfo Save(WarmupModel model)
        {
            return WarmupDALFactory.Create(LanguageID).Save(model);
        }

        public PagingList GetGetPublishedWarmupModels(int currentPageIndex, int pageSize, string sortType, string orderDirect)
        {
            IDALWarmup dal = WarmupDALFactory.Create(LanguageID);
            SortOrderPair sortOrder = (orderDirect == null || orderDirect == "0") ? SortOrder.Desc : SortOrder.Asc;
            SortingParameter sortingParms = null;
            switch (sortType)
            {
                case "1":   // 页面上叫喜欢，实际是投票
                    sortingParms = dal.SortedByVoteCount(sortOrder);
                    break;
                case "2":   // 评论
                    sortingParms = dal.SortedByCommentCount(sortOrder);
                    break;
                case "3":   // 转发
                    sortingParms = dal.SortedByForwardCount(sortOrder);
                    break;
                default:
                    sortingParms = null;
                    break;
            }

            PagingParameter pagingParams = PagingParameterFactory.CreateInstance(true, currentPageIndex, pageSize, sortingParms);
            PagingList models = dal.GetPublishedWarmupModels(pagingParams);
            return models;
        }

        public PagingList GetAllPublishedWarmupModels(int currentPageIndex, int pageSize, string sortType, string orderDirect)
        {
            IDALWarmup dal = WarmupDALFactory.Create(LanguageID);
            SortOrderPair sortOrder = (orderDirect == null || orderDirect == "0") ? SortOrder.Desc : SortOrder.Asc;
            SortingParameter sortingParms = null;
            switch (sortType)
            {
                case "1":   // 页面上叫喜欢，实际是投票
                    sortingParms = dal.SortedByVoteCount(sortOrder);
                    break;
                case "2":   // 评论
                    sortingParms = dal.SortedByCommentCount(sortOrder);
                    break;
                case "3":   // 转发
                    sortingParms = dal.SortedByForwardCount(sortOrder);
                    break;
                default:
                    sortingParms = null;
                    break;
            }

            PagingParameter pagingParams = PagingParameterFactory.CreateInstance(true, currentPageIndex, pageSize, sortingParms);
            PagingList models = dal.GetAllPublishedWarmupModels(pagingParams);
            return models;
        }

        /// <summary>
        ///    为单个作品投票
        /// </summary>
        /// <param name="int? warmId"></param>
        /// <returns></returns>
        public ResultInfo SetVote(int? warmId)
        {
            IDALWarmup use = WarmupDALFactory.Create(LanguageID);
            return use.Vote(warmId);
        }
        

    }
}
